import {createWebHashHistory,createRouter} from "vue-router"
import Login from "../pages/login/login.vue"
import layout from "../pages/Layout/layout.vue"


const routes =[
    {
        path:'/',
        redirect:'/login'
    }, 
    // 登录
    {
        path: '/login',
        component: Login,
    
    },
    // 首页
    {
        path: '/main',
        component: layout,
        children:[
            {
                path: '/main',
                component: () => import('../pages/Main.vue')
            }
        ]

    },
    {
        path: '/account',
        component: layout ,
        children:[
            {
                path: '/accountlist',
                component: () => import('../pages/account/AccountList.vue')
            }, {
                path: '/addaccount',
                component: () => import('../pages/account/AddAccount.vue')
            }, {
                path: '/personcenter',
                component: () => import('../pages/account/Center.vue')
            },{
                path: '/changepassword',
                component: () => import('../pages/account/revisepasw.vue')
            }
        ]
    }, {
        path: '/goods',
        component: layout ,
        children:[
            {
                path: '/goodsadd',
                component: () => import('../pages/goods/GoodsAdd.vue')
            }, {
                path: '/gooslist',
                component: () => import('../pages/goods/GoodsList.vue')
            }, {
                path: '/goostype',
                component: () => import('../pages/goods/GoodsType.vue')
            },
            {
                path: '/goodsedit',
                component: () => import('../pages/goods/GoodsEdit.vue') 
            }
        ]
    },
    //管理
    {
        path: '/order',
        component: layout,
        children:[
            {
                path: '/ordersManage',
                 component:() => import('../pages/manage/OrdersManage.vue'),
            },
            {
                path: '/shopManagement',
                component:() => import('../pages/manage/shopManagement.vue'),
            }
        ]
    },
    // 统计数据
    {
        path: '/Count',
        component: layout,
        children:[
            {
                path: '/shopCount',
                component:() => import('../pages/count/ShopCount.vue')
            },{
                path: '/orderCount',
                component:() => import('../pages/count/OderCount.vue')
            }
        ]
    
    },{
        path:'/:pathMatch(.*)*',
        component: () => import('../pages/error/Error.vue')
    }
    
]

const router = createRouter({
    history:createWebHashHistory(),routes
})

router.beforeEach((to, from) => {
    // 排除 不需要登录的页面，如果是不需要登录的页面  直接去
    let noLogin = ['/login']
    if (noLogin.includes(to.path)) {
        return true;
    }
    if (sessionStorage.getItem('accountid')) {
        // 放行
        return true;
    }
    return false

    //to: 要去的页面的信息
    //from: 从哪个页面来的信息
    // next: 放行函数   如果能够去这个页面  next()  如果不能去这个页面  next('/login')
    //next 是可选的  可以通过return true === next()     return false === next('/login')
})
export default router